<?php
/**
 * Created by PhpStorm.
 * Author: yostar
 * User: THINK
 * Date: 2018/6/2
 * Time: 15:46
 */

namespace wechat\model;

require_once 'Common.php';

class Material extends Common
{
    /**
     * 上传临时素材
     * @param $materialUrl
     * @return mixed|null
     */
    public function uploadTemporaryMaterial($materialUrl)
    {
        $postUrl = $this->formatUrlWithToken($this->wechat_config['uploadTemporaryMaterialUrl']);
        // php5.6及以上curl上传要求用CURLFile
        $data = [
            'media' => new \CURLFile($materialUrl),
        ];

        $result = $this->curl_post($data, $postUrl);
        if ($result) {
            if (isset($result['errcode'])) {
                return null;
            }
            return $result;
        } else {
            return null;
        }
    }

    /**
     * 上传除图文外其他永久素材 （视频上传未完成）
     * @param $materialUrl
     * @param $type
     * @return mixed|null
     */
    public function uploadPermanentMaterial($materialUrl, $type)
    {
        $url = sprintf($this->wechat_config['uploadPermanentMaterialUrl'], $this->get_access_token(), $type);

        $data = [
            'media' => new \CURLFile($materialUrl),
        ];
        $result = $this->curl_post($data, $url);

        return $result;

    }

    /**
     * 获取永久素材列表
     * @param $type
     * @param $offset
     * @param $count
     * @return mixed|null
     */
    public function getMaterialList($type, $offset, $count)
    {
        $data = [
            'type' => $type,
            'offset' => $offset,
            'count' => $count,
        ];
        $url = $this->formatUrlWithToken($this->wechat_config['getMaterialListUrl']);
        $result = $this->curl_post($data, $url);
        return $result;
    }

    /**
     * 获取各类永久素材总数
     * @return mixed
     */
    public function getMaterialCount()
    {
        $url = $this->formatUrlWithToken($this->wechat_config['getMaterialTotalUrl']);
        $result = $this->curl_get($url);
        return $result;
    }

    /**
     * 删除永久素材
     * @param $media_id
     * @return bool|mixed|null
     */
    public function deleteMaterial($media_id)
    {
        $data = [
            'media_id' => $media_id
        ];
        $url = $this->formatUrlWithToken($this->wechat_config['deleteMaterialUrl']);
        $result = $this->curl_post($data, $url);

        if (isset($result['errcode']) && $result['errcode'] == 0) {
            return $result;
        } else {
            return false;
        }
    }

    /**
     * 获取永久素材 (未测试)
     * @param $media_id
     * @return bool|mixed|null
     */
    public function getMaterial($media_id, $type)
    {
        $data = [
            'media_id' => $media_id,
        ];
        $url = $this->formatUrlWithToken($this->wechat_config['getMaterialUrl']);
        $result = $this->curl_post($data, $url);
        if (isset($result['errcode'])) {
            return false;
        } else {
            if ($type == 'news' || $type == 'video') {
                return $result;
            } else {
                // 保存为文件
                $res = $this->saveAsFile('../public/asset/images/copy.jpg', $result);
                if ($res) {
                    return '保存成功';
                } else {
                    return '保存失败';
                }
            }
        }
    }

    /**
     * 修改永久图文素材 (未测试)
     * @param $media_id
     * @param int $index
     * @param array $articles
     * @return bool|mixed|null
     */
    public function updateNews($media_id, $index = 0, $articles = [])
    {
        $data = [
            'media_id' => $media_id,
            'index' => $index,
            'articles' => $articles,
        ];
        $url = $this->formatUrlWithToken($this->wechat_config['updateNewsUrl']);
        $result = $this->curl_post($data, $url);
        if (isset($result['errcode']) && $result['errcode'] == 0) {
            return $result;
        } else {
            return false;
        }
    }

    /**
     * 新增永久图文素材 （未测试）
     * @param array $articles
     * @return bool|mixed|null
     */
    public function addNews($articles = [])
    {
        $url = $this->formatUrlWithToken($this->wechat_config['addNewsUrl']);
        $result = $this->curl_post($articles, $url);
        if (isset($result['media_id'])) {
            return $result;
        } else {
            return false;
        }
    }

    /**
     * 保存为文件 （未测试）
     * @param $filename
     * @param $filecontent
     * @return bool
     */
    private function saveAsFile($filename, $filecontent)
    {
        $file = fopen($filename, 'w');
        if ($file) {
            $result = fwrite($file, $filecontent);
            if ($result) {
                fclose($file);
                return true;
            } else {
                return false;
            }
        } else {
            echo false;
        }
    }
}